package com.lancoo.znbkxx.teacher.diagnosis

import android.annotation.SuppressLint
import androidx.core.os.bundleOf
import androidx.fragment.app.FragmentTransaction
import androidx.fragment.app.add
import androidx.fragment.app.commit
import com.lancoo.znbkxx.teacher.Ext
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.MyToast
import com.lancoo.znbkxx.teacher.base.BaseActivity
import com.lancoo.znbkxx.teacher.net.BkApiMethods
import com.lancoo.znbkxx.teacher.utils.LancooUtils
import kotlinx.android.synthetic.main.activity_plates_detail.*
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.util.concurrent.TimeoutException

/**
 * @author : Admin
 * @date   : 2020/12/17
 * @desc   :
 */
class SelfPlatesActivity : BaseActivity() {
    private val zsdArea by lazy { intent.getStringExtra("zsdarea") }
    private val stuID by lazy { intent.getStringExtra("stuID") }
    override fun setLayout(): Int {
        return R.layout.activity_plates_detail
    }

    override fun initView() {
        iv_back.setOnClickListener { finish() }
        loadingDialog.show()
        getPlates()
    }

    override fun isNeedToolBar(): Boolean {
        return false
    }

    @SuppressLint("CheckResult")
    private fun getPlates() {
        val method = BkApiMethods(this)
        val params = BkApiMethods.Paramas()
        params["ZsdArea"] = zsdArea
        params["StuID"]=stuID
        method.getStuCognitivePlate(Ext.RZZD_URL, params).subscribe(
                {
                    loadingDialog.dismiss()
                    if (it.Code == 1) {
                        supportFragmentManager.commit {
                            add<SelfPlatesFragment>(R.id.fcv_listen, null, bundleOf(Pair("type", 0),
                                    Pair("plates", it.Data[0])))
                            setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                        }
                        supportFragmentManager.commit {
                            add<SelfPlatesFragment>(R.id.fcv_read, null, bundleOf(Pair("type", 1),
                                    Pair("plates", it.Data[1])))
                            setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                        }
                    } else {
                        MyToast.showToast( it.Message)
                    }
                }, {
            loadingDialog.dismiss()
            val error: String = if (it is TimeoutException || it is SocketTimeoutException
                    || it is ConnectException
            ) {
                getString(R.string.aibk_network_timeout)
            } else {
                getString(R.string.aibk_server_sick)
            }
            MyToast.showToast( error)
        }
        )
    }
}